//
//  ConOldAddImageTextController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/9/4.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConOldAddImageTextController.h"
#import "YYTextView.h"
#import <UIButton+WebCache.h>
#import "PhotoButton.h"
#import "TakePhotoTool.h"
#import "UIAlertView+Entension.h"

@interface ConOldAddImageTextController ()<YYTextViewDelegate>

@property (nonatomic,strong) NSMutableArray *images;
@property (nonatomic,weak) UIView *imgViews;
@property (nonatomic,copy) OldImageDetail *copModel;

@end

@implementation ConOldAddImageTextController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新增图文介绍";
    if (self.isAdd) {
        self.copModel = [[OldImageDetail alloc]init];
    }else {
        self.copModel = [self.model copy];
    }
    [self setUpNavi];
    [self setUpView];
}
- (void)setUpNavi
{
    UIButton *editBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [editBtn setTitle:@"完成" forState:UIControlStateNormal];
    editBtn.tag = 0;
    [editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    editBtn.titleLabel.font = Font(16);
    [editBtn addTarget:self action:@selector(completeClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:editBtn];
}
- (void)completeClick
{
    if (!self.isAdd) {
        [self.model replaceTheOldModel:self.copModel];
    }
    if (self.completeBlock) {//新增的需要带回去 加在数组中
        self.completeBlock(self.copModel);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setUpView
{
    YYTextView *textView = [[YYTextView alloc]initWithFrame:CGRectMake(10, 10, ScreenW-20, 80)];
    textView.placeholderText = @"输入内容";
    textView.font = Font(15);
    textView.placeholderFont = Font(15);
    textView.delegate = self;
    [self.view addSubview:textView];
    if (self.copModel.dynamicImageContent) {
        textView.text = self.copModel.dynamicImageContent;
    }
    
    CGFloat imageW = (ScreenW-20*4)/3.0;
    UIView *imgViews = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(textView.frame)+15, ScreenW, imageW)];
    self.imgViews = imgViews;
    [self.view addSubview:imgViews];
    [self setUpImageView];
}
- (void)setUpImageView
{
    CGFloat imageW = (ScreenW-20*4)/3.0;
    CGFloat minX = 20;
    for (NSInteger i=0; i<self.copModel.dynamicImageList.count; i++) {
        PhotoButton *photoView = [[PhotoButton alloc]initWithFrame:CGRectMake(minX, 0, imageW, imageW)];
        [photoView sd_setBackgroundImageWithURL:[NSURL URLWithString:self.copModel.dynamicImageList[i]] forState:UIControlStateNormal placeholderImage:GetImage(@"上传图片")];
        photoView.isHaveImg = YES;
        photoView.tag = i;
        [self.imgViews addSubview:photoView];
        [photoView addTarget:self action:@selector(delectClick:) forControlEvents:UIControlEventTouchUpInside];
        minX += imageW+20;
    }
    if (self.copModel.dynamicImageList.count<3) {
        PhotoButton *photoView = [[PhotoButton alloc]initWithFrame:CGRectMake(minX, 0, imageW, imageW)];
        [photoView setBackgroundImage:GetImage(@"上传图片") forState:UIControlStateNormal];
        [photoView addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchUpInside];
        photoView.tag = self.copModel.dynamicImageList.count;
        [self.imgViews addSubview:photoView];
    }
}
- (void)removeAllImgs
{
    [self.imgViews.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
- (void)reloadImgView
{
    [self removeAllImgs];
    [self setUpImageView];
}
- (void)delectClick:(PhotoButton *)btn
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确定要删除吗" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert handlerClickedButton:^(UIAlertView *alertView, NSInteger btnIndex) {
        if (btnIndex == 1) {
            [self.copModel.dynamicImageList removeObjectAtIndex:btn.tag];
            [self reloadImgView];
        }
    }];
    [alert show];
}
- (void)addClick:(PhotoButton *)btn
{
    [[TakePhotoTool shareTool] beginSelectWithScale:0 image:^(UIImage *image, NSString *path) {
        [self.copModel.dynamicImageList addObject:path];
        [self reloadImgView];
    }];
}
- (void)textViewDidChange:(YYTextView *)textView
{
    self.copModel.dynamicImageContent = textView.text;
}

- (NSMutableArray *)images
{
    if (!_images) {
        _images = [NSMutableArray array];
    }
    return _images;
}


@end
